/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.apt.loginbean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.Session;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apt.connectDB.MyConnect;

/**
 *
 * @author MinhThang
 */
public class LoginBean extends org.apache.struts.action.ActionForm {
    
    private String txtUserName;
    private String txtPassword;
    /**
     *
     */
    public LoginBean() {
        super();
        // TODO Auto-generated constructor stub
    }
//Member login
    public boolean Login(String user,String pass){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn=MyConnect.getConnection();
            String sql="select * from Member where UserName=? and Password=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, user);
            ps.setString(2, pass);
            rs=ps.executeQuery();
            if(rs.next()){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getTxtUserName() == null || getTxtUserName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
        }
        if (getTxtPassword() == null || getTxtPassword().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
        }
        return errors;
    }

    /**
     * @return the txtUserName
     */
    public String getTxtUserName() {
        return txtUserName;
    }

    /**
     * @param txtUserName the txtUserName to set
     */
    public void setTxtUserName(String txtUserName) {
        this.txtUserName = txtUserName;
    }

    /**
     * @return the txtPassword
     */
    public String getTxtPassword() {
        return txtPassword;
    }

    /**
     * @param txtPassword the txtPassword to set
     */
    public void setTxtPassword(String txtPassword) {
        this.txtPassword = txtPassword;
    }
}
